import "../../css/help.css";
import REST from "@module/Rest";
import HelpColumns from "@root/P/components/HelpColumns";
import ZenUI from "@root/components/zenUi/ZenUI";

function HelpCenter(props) {

    //初始化插件入参
    const {} = props;

    //初始化插件状态值及状态更新方法
    const [helpStatus, updateHelpStatus] = usePartState({
        loadData:[],
        search:$state.params.hasOwnProperty("search")?$state.params.search:""
    })

    console.log("HelpCenter",$state.params.search,helpStatus);

    //更新视图数据 私有的方法前缀用_
    function _updateData(value) {
        let newValue = { ...helpStatus, ...value };
        console.log(newValue);
        updateHelpStatus(newValue);
    }

    //更新视图数据方法外部接口
    function updateLoadFunctions(newStatus){
        _updateData(
            (helpStatus.loadData = newStatus)
        )
    }

    //更新搜索内容外部接口
    function updateSearchFunctions(newStatus){
        _updateData(
            (helpStatus.search = newStatus)
        )
    }

    //获取帮助中心栏目数据
    function helpColumnsData(inputData){
        let url = "/cms/notice/getNoticesBySecColumnType";
        REST.post(url,inputData).then(
            data => {
                if (data.msg == "success"||data.code=="200") {
                    updateLoadFunctions(data.data);
                    //updateHelpStatus({loadData:data.data});
                }else{
                    ZenUI.ST(data.msg, 2);
                }
            },
            error => {}
        );
    }

    function checkSearch(){
        let searchInput = $state.params.search;
        console.log(searchInput,helpStatus.search);
        if(searchInput != helpStatus.search){
            let orgId = Uts.getCookie("orgId");
            let suppId = Uts.getCookie("suppId");
            let inputData = {}
            if(orgId&&orgId!=undefined&&orgId!=""){
                inputData.columnType = 3;
            }else if(suppId&&suppId!=undefined&&suppId!=""){
                inputData.columnType = 4;
            }else{
                inputData.columnType = 2
            }
            if(searchInput != ""&&searchInput != undefined){
                inputData.title = searchInput;
            }
            //inputData.columnType = 0;//测试
            console.log("checkSearch",inputData);
            updateSearchFunctions(searchInput);
            helpColumnsData(inputData);
        }
    }

    checkSearch();

    //componentDidMount
    React.useEffect(() => {
        console.log("refresh view");
        let orgId = Uts.getCookie("orgId");
        let suppId = Uts.getCookie("suppId");
        let inputData = {}
        if(orgId&&orgId!=undefined&&orgId!=""){
            inputData.columnType = 3;
        }else if(suppId&&suppId!=undefined&&suppId!=""){
            inputData.columnType = 4;
        }else{
            inputData.columnType = 2
        }
        if(helpStatus.search != ""&&helpStatus.search != undefined){
            inputData.title = helpStatus.search
        }
        //inputData.columnType = 0; //测试
        console.log("search",inputData,helpStatus.search);
        helpColumnsData(inputData);
        console.log(helpStatus);
    },[]);

    return (
        <div className="zen_w-p100 zen_p-b-50 zen_p-t-30" style={{backgroundColor:"#F0F5F8",minHeight:"700px"}}>
            <div className="zen_w-1200 zen_center">
                {helpStatus.loadData&&helpStatus.loadData.length>0?helpStatus.loadData.map((item,index) =>{
                    {return <div className="zen_li-bl zen_w-p30 zen_m-30 zen_v-t zen_h-250 zen_m-r-0" key={index} style={{"display":"inline-block"}}><HelpColumns
                        inputdata = {item}
                    /></div>}
                }):<div className="zen_p-10 zen_bg-white zen_text zen_t-c zen_fs-20" style={{minHeight:"700px"}}>
                    <h3 style={{lineHeight:"700px"}}>暂未找到相关信息哦～</h3>
                </div>}
            </div>
        </div>
    )
}

export default HelpCenter;